package com.weishaoying.consumer.web;

import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.weishaoying.api.entity.User;
import com.weishaoying.api.enums.GenderEnum;
import com.weishaoying.api.service.UserThriftService;
import org.apache.thrift.TException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author WeiShaoying
 * @date 2018/9/27
 */
@RestController
public class TestController {

    @Resource
    private UserThriftService userThriftService;

    private Random random = new Random();

    @GetMapping(value = "/")
    private String hello() {
        return "Hello-" + random.nextInt(10000);
    }

    @GetMapping(value = "/query")
    public User queryUser() {
        try {
            return userThriftService.queryUserByGender(GenderEnum.GENDER_FEMALE);
        } catch (TException e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping(value = "/list/{ids}")
    public List<User> queryUserList(@PathVariable String ids) {
        List idList = Lists.newArrayList(Splitter.on(',')
                .trimResults()
                .omitEmptyStrings()
                .split(ids));
        List list = Lists.transform(idList, new Function<String, Integer>() {
            @Nullable
            @Override
            public Integer apply(@Nullable String input) {
                return StringUtils.hasText(input) ? Integer.parseInt(input) : 0;
            }
        });
        System.out.println("list = " + list);
        try {
            return userThriftService.queryUsersByIdList(list);
        } catch (TException e) {
            e.printStackTrace();
        }
        return null;
    }
}
